package com.example.mybatisplus.util;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.example.init.SqltoyConfig;
import com.example.mybatisplus.mapper.UserMapper;
import com.example.util.ConfigUtil;
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

public class MyBatisPlusUtil {
    private static SqlSessionFactory sqlSessionFactory;

//    static {
//        SqltoyConfig sqltoyConfig = ConfigUtil.getConfig("mysql", SqltoyConfig.class);
//        // 创建数据源
//        PooledDataSource dataSource = new PooledDataSource(
//                sqltoyConfig.getDriverClassName(),
//                sqltoyConfig.getUrl(),
//                sqltoyConfig.getUsername(),
//                sqltoyConfig.getPassword()
//        );
//
//        // 创建事务工厂
//        TransactionFactory transactionFactory = new JdbcTransactionFactory();
//
//        // 创建环境
//        Environment environment = new Environment.Builder("development")
//                .transactionFactory(transactionFactory)
//                .dataSource(dataSource)
//                .build();
//
//        // 创建Configuration对象
//        Configuration configuration = new Configuration(environment);
//
//        // 添加Mapper接口
//        configuration.addMapper(UserMapper.class);
//
//        // 构建SqlSessionFactory
//        sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
//    }

    public static SqlSessionFactory initSqlSessionFactory() throws IOException {
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setMapUnderscoreToCamelCase(true); // 开启驼峰映射
        configuration.setUseGeneratedKeys(true); // 开启主键自动生成

        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setDbConfig(new GlobalConfig.DbConfig());
        GlobalConfigUtils.setGlobalConfig(configuration, globalConfig);

        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); // 添加分页插件
        configuration.addInterceptor(interceptor);

        Environment environment = new Environment("development",
                new JdbcTransactionFactory(),
                initDataSource());
        configuration.setEnvironment(environment);

        // 加载 Mapper XML 文件
        List<String> mapperLocations = new ArrayList<>();
        mapperLocations.add("classpath*:com/yourproject/mapper/*.xml");
        for (String location : mapperLocations) {
            try (Reader reader = Resources.getResourceAsReader(location)) {
                XMLMapperBuilder mapperBuilder = new XMLMapperBuilder(reader, configuration, location, configuration.getSqlFragments());
                mapperBuilder.parse();
            }
        }

        configuration.addMapper(UserMapper.class);
//        return new SqlSessionFactoryBuilder().build(configuration);
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
        return sqlSessionFactory;
    }

    public static DataSource initDataSource() {
        SqltoyConfig sqltoyConfig = ConfigUtil.getConfig("mysql", SqltoyConfig.class);
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(sqltoyConfig.getUrl());
        dataSource.setUsername(sqltoyConfig.getUsername());
        dataSource.setPassword(sqltoyConfig.getPassword());
        dataSource.setDriverClassName(sqltoyConfig.getDriverClassName());
        return dataSource;
    }
    public static SqlSessionFactory getSqlSessionFactory() {
        return sqlSessionFactory;
    }
}